Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 22-Sep-2010 (Jagdish)

Public Class frmPracticeInformation
    Dim mstrMode As String
    Public blnAdd As Boolean, blnEdit As Boolean, blnDelete As Boolean, blnLocked As Boolean
    Dim mlngRecordID As Long
    Dim blnUnload As Boolean
    Dim mblnChanged As Boolean
    Dim mblnActivate As Boolean
    Dim mblnExpanded As Boolean = False
    Dim mblnAutoSize As Boolean = True
    Dim gridView1 As Views.Grid.GridView
    Dim mblnCancelDisplay As Boolean

    Private Sub frmPracticeInformation_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            If mblnActivate = True Then
                mblnActivate = False

                'Fill Town combo
                Call PopulateLookupDX(lkTown, "Town", "TownId", "LtblTown", "TownId,Town", , "Town")
                'Fill Parish combo
                Call PopulateLookupDX(lkParish, "Parish", "ParishId", "LtblParish", "ParishId,Parish", , "Parish")
                'Fill Country combo
                Call PopulateLookupDX(lkCountry, "Country", "CountryId", "LtblCountry", "CountryId,Country", , "Country")
                'Fill FirstAppointmentTime combo
                Call PopulateLookupDX(lkFirstAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblFirstAppointmentTimes", "AppointmentTimeId,AppointmentTime")
                lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
                lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"
                'Fill LastAppointmentTime combo
                Call PopulateLookupDX(lkLastAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblLastAppointmentTimes", "AppointmentTimeId,AppointmentTime")
                lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
                lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"

                Call DisplayData()
            End If

        Catch ex As Exception
            pbRecords.Visible = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmPracticeInformation_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try

            Dim intUnload As DialogResult

            If mblnChanged = True And mstrMode <> "" Then
                intUnload = MessageBox.Show("You have not saved your changes to the record. Do you want to save your changes now?", gstrMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                If intUnload = Windows.Forms.DialogResult.Cancel Then
                    e.Cancel = True : Exit Sub
                ElseIf intUnload = Windows.Forms.DialogResult.Yes Then
                    blnUnload = True
                    If blnSaveRecords() = False Then
                        e.Cancel = True
                        blnUnload = False
                        Exit Sub
                    End If
                    mblnChanged = False
                End If
            End If
            blnUnload = False

        Catch ex As Exception
            blnUnload = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmPracticeInformation_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Call SetImageControl(Me)
            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgFormIcons.Images(2), Bitmap).GetHicon())
            Call GetAccessPermission(Me, "frmPracticeInformation")
            mblnActivate = True
            mblnChanged = False

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub


    Private Sub DisplayData()
        'Display data in form fields.
        Dim dsTemp As DataSet
        Dim strQuery As String
        Try
            strQuery = "Select U.FirstName,U.LastName,U.Qualifications,P.CompanyName,P.Address1,P.Address2, " & _
                        "P.TownId,P.ParishId,P.CountryId,P.PhoneNo,P.FaxNo,P.PhysicianLicenceNumber," & _
                        "P.FirstAppointmentTimeId,P.LastAppointmentTimeId,P.AppointmentDuration " & _
                        "from tblUsers U,tblPracticeInfo P where U.UserId=P.UserId and P.UserId=" & gintUserID
            dsTemp = FillDataSet(strQuery)

            With dsTemp.Tables(0)
                If .Rows.Count > 0 Then
                    txtFirstName.Text = Trim(.Rows(0).Item("FirstName") & "")
                    txtLastName.Text = Trim(.Rows(0).Item("LastName") & "")
                    txtQualification.Text = Trim(.Rows(0).Item("Qualifications") & "")
                    txtCompanyName.Text = Trim(.Rows(0).Item("CompanyName") & "")
                    txtAddress1.Text = Trim(.Rows(0).Item("Address1") & "")
                    txtAddress2.Text = Trim(.Rows(0).Item("Address2") & "")
                    lkTown.EditValue = CInt(.Rows(0).Item("TownId"))
                    lkParish.EditValue = CInt(.Rows(0).Item("ParishId"))
                    lkCountry.EditValue = CInt(.Rows(0).Item("CountryId"))
                    txtPhoneNo.Text = Trim(.Rows(0).Item("PhoneNo") & "")
                    txtFax.Text = Trim(.Rows(0).Item("FaxNo") & "")
                    txtLicenseNumber.Text = Trim(.Rows(0).Item("PhysicianLicenceNumber") & "")
                    lkFirstAppointmentTime.EditValue = CInt(.Rows(0).Item("FirstAppointmentTimeId"))
                    lkLastAppointmentTime.EditValue = CInt(.Rows(0).Item("LastAppointmentTimeId"))
                    cboDuration.Text = .Rows(0).Item("AppointmentDuration")
                End If
            End With

            dsTemp.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False

        If IsDBNullOrNothing(txtFirstName.Text) Then
            strMsg = "Please enter First Name"
            txtFirstName.Focus()
        ElseIf IsDBNullOrNothing(txtLastName.Text) Then
            strMsg = "Please enter Last Name"
            txtLastName.Focus()
        ElseIf IsDBNullOrNothing(lkTown.EditValue) Then
            strMsg = "Please select Town"
            lkTown.Focus()
        ElseIf IsDBNullOrNothing(cboDuration.Text) Then
            strMsg = "Please select Appointment Duration"
            cboDuration.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Function blnSaveRecords() As Boolean
        Dim blnIsTransactionOpen As Boolean
        Dim strQuery As String
        Try
            blnSaveRecords = False

            If IsValidEntry() = False Then
                Exit Function
            End If

            If (blnUnload = False) Then
                If (MessageBox.Show("Are you sure to " & IIf(mstrMode = "Add", "add", "modify") & " this practice information?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.No) Then
                    txtFirstName.Focus()
                    Exit Function
                End If
            End If

            gtrnWebPlusMed = gconWebPlusMed.BeginTransaction
            blnIsTransactionOpen = True
            strQuery = "FirstName='" & Replace(Trim(txtFirstName.Text), "'", "''") & "'"
            strQuery = strQuery & ",LastName='" & Replace(Trim(txtLastName.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",Qualifications='" & Replace(Trim(txtQualification.Text & ""), "'", "''") & "'"
            Call UpdateTable("tblUsers", strQuery, "UserId=" & gintUserID)

            strQuery = "CompanyName='" & Replace(Trim(txtCompanyName.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",Address1='" & Replace(Trim(txtAddress1.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",Address2='" & Replace(Trim(txtAddress2.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",TownId=" & Val(lkTown.EditValue)
            strQuery = strQuery & ",ParishId=" & Val(lkParish.EditValue)
            strQuery = strQuery & ",CountryId=" & Val(lkCountry.EditValue)
            strQuery = strQuery & ",PhoneNo='" & Replace(Trim(txtPhoneNo.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",FaxNo='" & Replace(Trim(txtFax.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",PhysicianLicenceNumber='" & Replace(Trim(txtLicenseNumber.Text & ""), "'", "''") & "'"
            strQuery = strQuery & ",FirstAppointmentTimeId=" & Val(lkFirstAppointmentTime.EditValue)
            strQuery = strQuery & ",LastAppointmentTimeId=" & Val(lkLastAppointmentTime.EditValue)
            strQuery = strQuery & ",AppointmentDuration=" & Val(cboDuration.Text)
            Call UpdateTable("tblPracticeInfo", strQuery, "UserId=" & gintUserID)
            blnSaveRecords = True

            gtrnWebPlusMed.Commit()
            blnIsTransactionOpen = False

        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
            End If
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Function
    Public Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If mblnChanged = False Then Exit Sub
            If blnSaveRecords() = False Then Exit Sub

        Catch ex As Exception
            pbRecords.Visible = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub txtFirstName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirstName.GotFocus, txtLastName.GotFocus, txtAddress1.GotFocus, txtAddress2.GotFocus, txtFax.GotFocus, txtPhoneNo.GotFocus, txtQualification.GotFocus, txtLicenseNumber.GotFocus, txtCompanyName.GotFocus
        sender.SelectAll()
    End Sub

    Private Sub ClearControls()
        Try

            Call Clearfields(grbControls)

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub lkTown_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkTown.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookupDX(lkTown, "Town", "TownId", "LtblTown", "TownId,Town", , "Town")
        End If
    End Sub

    Private Sub lkTown_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lkTown.EditValueChanged
        mblnChanged = True
        If Not IsDBNullOrNothing(lkTown.EditValue) Then
            lkParish.EditValue = CInt(Val(strRecordID("LtblTown", "ParishId", "TownId=" & Val(lkTown.EditValue))))
        End If
    End Sub

    Private Sub lkParish_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkParish.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookupDX(lkParish, "Parish", "ParishId", "LtblParish", "ParishId,Parish", , "Parish")
        End If
    End Sub

    Private Sub lkParish_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lkParish.EditValueChanged
        mblnChanged = True
        If Not IsDBNullOrNothing(lkParish.EditValue) Then
            lkCountry.EditValue = CInt(Val(strRecordID("LtblParish", "CountryId", "ParishId=" & Val(lkParish.EditValue))))
        End If
    End Sub

    Private Sub lkCountry_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkCountry.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookupDX(lkCountry, "Country", "CountryId", "LtblCountry", "CountryId,Country", , "Country")
        End If
    End Sub

    Private Sub lkFirstAppointmentTime_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkFirstAppointmentTime.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookupDX(lkFirstAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblFirstAppointmentTimes", "AppointmentTimeId,AppointmentTime")
            lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
            lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"
        End If
    End Sub

    Private Sub lkLastAppointmentTime_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkLastAppointmentTime.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookupDX(lkLastAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblLastAppointmentTimes", "AppointmentTimeId,AppointmentTime")
            lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
            lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"
        End If
    End Sub

    Private Sub lkFirstAppointmentTime_ProcessNewValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs) Handles lkFirstAppointmentTime.ProcessNewValue
        If IsDBNullOrNothing(e.DisplayValue) = True Then Exit Sub
        If MessageBox.Show("This value is not present in the list. Do you want to add it now?", gstrMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            lkFirstAppointmentTime.EditValue = Nothing
            lkFirstAppointmentTime.Focus()
            Exit Sub
        End If
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblFirstAppointmentTimes"
            .mintGrbId = 1
            .mstrItemName = Format(e.DisplayValue, "hh:mm tt")
            .ShowDialog()
            Call PopulateLookupDX(lkFirstAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblFirstAppointmentTimes", "AppointmentTimeId,AppointmentTime")
            lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
            lkFirstAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"
            If mlngLookUpId > 0 Then
                lkFirstAppointmentTime.EditValue = CInt(mlngLookUpId)
            Else
                lkFirstAppointmentTime.EditValue = Nothing
                lkFirstAppointmentTime.Focus()
            End If

            .Dispose()
            GC.Collect()
        End With
        mlngLookUpId = Nothing
    End Sub

    Private Sub lkLastAppointmentTime_ProcessNewValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs) Handles lkLastAppointmentTime.ProcessNewValue
        If IsDBNullOrNothing(e.DisplayValue) = True Then Exit Sub
        If MessageBox.Show("This value is not present in the list. Do you want to add it now?", gstrMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            lkLastAppointmentTime.EditValue = Nothing
            lkLastAppointmentTime.Focus()
            Exit Sub
        End If
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblLastAppointmentTimes"
            .mintGrbId = 1
            .mstrItemName = Format(e.DisplayValue, "hh:mm tt")
            .ShowDialog()
            Call PopulateLookupDX(lkLastAppointmentTime, "AppointmentTime", "AppointmentTimeId", "LtblLastAppointmentTimes", "AppointmentTimeId,AppointmentTime")
            lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatType = DevExpress.Utils.FormatType.DateTime
            lkLastAppointmentTime.Properties.Columns("AppointmentTime").FormatString = "hh:mm tt"
            If mlngLookUpId > 0 Then
                lkLastAppointmentTime.EditValue = CInt(mlngLookUpId)
            Else
                lkLastAppointmentTime.EditValue = Nothing
                lkLastAppointmentTime.Focus()
            End If

            .Dispose()
            GC.Collect()
        End With
        mlngLookUpId = Nothing
    End Sub

    Private Sub txtFirstName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.TextChanged, txtAddress1.TextChanged, txtAddress2.TextChanged, txtCompanyName.TextChanged, txtFax.TextChanged, txtLastName.TextChanged, txtLicenseNumber.TextChanged, txtPhoneNo.TextChanged, txtQualification.TextChanged, lkLastAppointmentTime.TextChanged, lkFirstAppointmentTime.TextChanged, lkCountry.TextChanged, cboDuration.TextChanged
        mblnChanged = True
    End Sub

    Private Sub lkTown_ProcessNewValue(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs) Handles lkTown.ProcessNewValue
        If IsDBNullOrNothing(e.DisplayValue) = True Then Exit Sub
        If MessageBox.Show("This value is not present in the list. Do you want to add it now?", gstrMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            lkTown.EditValue = Nothing
            lkTown.Focus()
            Exit Sub
        End If
        With frmToolsLookUp
            .mblnFrmOtherForm = True
            .mstrLookUpValue = "LtblTown"
            .mintGrbId = 2
            .mstrItemName = e.DisplayValue
            .ShowDialog()
            Call PopulateLookupDX(lkTown, "Town", "TownId", "LtblTown", "TownId,Town", , "Town")
            If mlngLookUpId > 0 Then
                lkTown.EditValue = CInt(mlngLookUpId)
            Else
                lkTown.EditValue = Nothing
                lkTown.Focus()
            End If

            .Dispose()
            GC.Collect()
        End With
        mlngLookUpId = Nothing
    End Sub

    Private Sub lkTown_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles lkTown.Validated
        If IsDBNullOrNothing(lkTown.EditValue) Then
            lkParish.EditValue = Nothing
            lkCountry.EditValue = Nothing
        End If
    End Sub
End Class
